//
//  ChangeMobilVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/3.
//  Copyright © 2020 tym. All rights reserved.
//

#import "ChangeMobilVC.h"
#import "UserService.h"
#import "XMPayCodeView.h"
#import "UIimage+Extends.h"
#import "UIButton+Gradient.h"
#import "MineSettingViewController.h"
@interface ChangeMobilVC ()


@property(nonatomic,weak) IBOutlet UITextField *mobileFid;
@property(nonatomic,weak) IBOutlet UITextField *codeFid;
@property(nonatomic,weak) IBOutlet UIButton *getCodeBtn;
@property(nonatomic,weak) IBOutlet UIButton *nextBtn;
@property(nonatomic,strong) UserService *userService;

@property(nonatomic,assign) NSInteger type; //0:old 1:new

@property(nonatomic,assign) NSInteger seconds;
@end

@implementation ChangeMobilVC

-(instancetype)initWithType:(NSInteger)type{
    self = [super init];
    if (self) {
        self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    
    self.seconds = 60;
    self.getCodeBtn.layer.cornerRadius = 3;
    self.getCodeBtn.layer.borderColor = rgba(74, 74, 74).CGColor;
    self.getCodeBtn.layer.borderWidth= 1.5;
    
    self.nextBtn.layer.cornerRadius = 5;
    self.nextBtn.layer.masksToBounds = YES;
    
    [self addTitle:@"修改手机号码"];
    [self.codeFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    
    if (self.type == 0) {
        self.mobileFid.text = [UserTool getLogInUser].mobile;
        [self.nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    }else{
        [self.nextBtn setTitle:@"确定" forState:UIControlStateNormal];
        [self.mobileFid setPlaceholder:@"输入新手机号"];
    }
}

-(UserService*)userService{
    if (_userService == nil) {
        _userService = [[UserService alloc] init];
    }
    return _userService;
}

-(void)changedTextField:(UITextField*)fid{
    if (self.mobileFid.text.length < 11 ||  self.codeFid.text.length < 4) {
        [self.nextBtn  setBackgroundImage:[UIImage imageWithColor:rgba(230, 230, 230) withSize:self.nextBtn.bounds.size] forState:UIControlStateNormal];
    }else{
        [self.nextBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    }
}

//开始倒计时
-(void)startCountDown{
    [self changeSecend];
}

-(void)changeSecend{
    if (_seconds > 0) {
        _seconds =_seconds-1;
        [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%lds",_seconds] forState:UIControlStateNormal];
        [self performSelector:@selector(changeSecend) withObject:self afterDelay:1];
    } else{
        [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
        _seconds = 60;
    }
}

-(void)getVerificationCode:(NSString*)mobile{
    if (mobile.length < 11) {
        [WHToast showMessage:@"请检查你输入的手机号码" duration:2 finishHandler:nil];
        return;
    }
    [MBProgressHUD wj_showActivityLoading:@"" toView:self.view];
    TYMWeak(self);
    [self.userService getValidteionCode:mobile completion:^(NSError * _Nonnull error, LMJBaseResponse * _Nonnull response) {
        [MBProgressHUD wj_hideHUDForView:weakself.view];
        if (response.code == 0) {
            [weakself startCountDown];
            [WHToast showMessage:@"验证码发送成功" duration:2 finishHandler:nil];
        }else{
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }
    }];
}

-(IBAction)getCodeAction:(id)sender{
    
    if(_seconds < 60){
        return;
    }
    [self getVerificationCode:self.mobileFid.text];
    
}

//
-(IBAction)newChangeModileAction:(id)sender{
    TYMWeak(self);
    if (self.type == 0) {
        
        [self.userService getVerificationCode:self.mobileFid.text validateionCode:self.codeFid.text completion:^(NSError * _Nonnull error, LMJBaseResponse * _Nonnull response) {
            if (response.code == 0) {
                ChangeMobilVC *changeMobile = [[ChangeMobilVC alloc] initWithType:1];
                [weakself.navigationController pushViewController:changeMobile animated:YES];
            }else{
                [WHToast showMessage:response.message duration:2 finishHandler:nil];
                
            }
        }];
    }else{
        
        [self.userService getUpdateUserMobile:self.mobileFid.text validateionCode:self.codeFid.text completion:^(NSError * _Nonnull error, LMJBaseResponse * _Nonnull response) {
            
            if (response.message) {
                [WHToast showMessage:response.message duration:2 finishHandler:^{
                }];
            }else{
                [WHToast showMessage:@"修改成功" duration:2 finishHandler:^{
                }];
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[MineSettingViewController class]]) {
                       [self.navigationController popToViewController:controller animated:YES];
                       return;
                    }
                }
            }
        }];
    }
}


@end
